import { Component, Injector, ViewChild, AfterViewInit, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { LocationServiceProxy, LocationOfMaterialListDto, AppLicationTreeDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';

import { CreateOrEditLocationModalComponent } from './create-or-edit-location-modal.component';

import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/table';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { TreeDataHelperService } from '@shared/utils/tree-data-helper.service';
import { IBasicOrganizationUnitInfo } from '@app/admin/organization-units/basic-organization-unit-info';

@Component({
  templateUrl: './location.component.html',
  styleUrls: ['./location.component.less'],
  animations: [appModuleAnimation()]
})

export class MaterialLocationComponent extends AppComponentBase implements AfterViewInit {

  @ViewChild('createOrEditLocationModal') createOrEditLocationModal: CreateOrEditLocationModalComponent;

  @ViewChild('dataTable') dataTable: Table;
  @Output() ouSelected = new EventEmitter<IBasicOrganizationUnitInfo>();


  areas = new Array<AppLicationTreeDto>();
  constructor(
    injector: Injector,
    private _http: Http,
    private _locationServiceProxy: LocationServiceProxy,
    private _arrayToTreeConverterService: ArrayToTreeConverterService,
    private _treeDataHelperService: TreeDataHelperService
  ) {
    super(injector);
  }

  ngAfterViewInit(): void {
    this.getAreas();
  }

  getAreas() {
    this._locationServiceProxy.getAreas("", true, 51).subscribe(result => {
      this.areas = result.items;
    });
  }

  deleteLocation( location: LocationOfMaterialListDto): void {
    this.message.confirm(
      '确认删除' + location.displayName + '吗',
      (isConfirmed) => {
        if (isConfirmed) {
          this._locationServiceProxy.deleteLocation(location.id)
            .subscribe(() => {
              this.getAreas();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );
  }
}
